/**
 * 文件名称:  ISAPDao.java
 * 版权信息:  ChinaStar Technologies Co., Ltd. Copyright 2011-2021, All rights reserved
 * 详细描述:  <描述>
 * 修改作者:  xdlid
 * 修改时间:  Feb 27, 2012
 */
package com.chinastar.iweb.common.jdbc;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
 * ISAPDao
 * 
 * @author xdlid
 * @version Mar 15, 2012
 * @see StarSupport
 * @since 1.0
 */
public class StarSupport
{
    private DataSource dataSource = null;
    
    private Connection connection = null;
    
    private CallableStatement callableStatement = null;
    
    private ResultSet resultSet = null;
    
    /**
     * setProcedure
     * 
     * @param procedureName
     * @throws SQLException
     */
    public void setProcedure(String procedureName) throws SQLException
    {
        connection = dataSource.getConnection();
        callableStatement = connection.prepareCall("{ call " + procedureName + "}");
    }
    
    /**
     * addParame
     * 
     * @param parameName
     * @param dataType
     * @param dataValue
     * @param parameType
     * @throws SQLException
     */
    public void addParame(String parameName, int dataType, Object dataValue, int parameType) throws SQLException
    {
        switch (parameType)
        {
            case ParameType.PARAME_IN:
                addInputParame(parameName, dataType, dataValue);
                break;
            
            default:
                callableStatement.registerOutParameter(parameName, dataType);
                break;
        }
    }
    
    /**
     * addInputParame
     * 
     * @param parameterName
     * @param dataType
     * @param dataValue
     * @throws SQLException
     */
    private void addInputParame(String parameterName, int dataType, Object dataValue) throws SQLException
    {
        StringBuffer dataValues = new StringBuffer();
        switch (dataType)
        {
            case DataType.CURSOR:
                callableStatement.setObject(parameterName, dataValue);
                break;
            
            case DataType.DOUBLE:
                dataValues.append(dataValue);
                callableStatement.setDouble(parameterName, Double.parseDouble(dataValues.toString()));
                break;
            
            case DataType.INTEGER:
                dataValues.append(dataValue);
                callableStatement.setInt(parameterName, Integer.parseInt(dataValues.toString()));
                break;
            
            default:
                callableStatement.setString(parameterName, dataValue.toString());
                break;
        }
    }
    
    /**
     * close
     */
    public void close()
    {
        try
        {
            if (null != resultSet)
            {
                resultSet.close();
            }
            if (null != callableStatement)
            {
                callableStatement.close();
            }
            if (null != connection)
            {
                connection.close();
            }
        }
        catch (Exception e)
        {
            
        }
    }
    
    /**
     * close
     * 
     * @param rs
     */
    public void close(ResultSet rs)
    {
        try
        {
            if (null != resultSet)
            {
                resultSet.close();
            }
            if (null != callableStatement)
            {
                callableStatement.close();
            }
            if (null != connection)
            {
                connection.close();
            }
        }
        catch (Exception e)
        {
            
        }
    }
    
    /**
     * execute
     * 
     * @throws SQLException
     */
    public void execute() throws SQLException
    {
        callableStatement.execute();
    }
    
    /**
     * getInt
     * 
     * @param parameterName
     * @return
     * @throws SQLException
     */
    public int getInt(String parameterName) throws SQLException
    {
        return callableStatement.getInt(parameterName);
    }
    
    /**
     * getString
     * 
     * @param parameterName
     * @return
     * @throws SQLException
     */
    public String getString(String parameterName) throws SQLException
    {
        return callableStatement.getString(parameterName);
    }
    
    /**
     * getDouble
     * 
     * @param parameterName
     * @return
     * @throws SQLException
     */
    public double getDouble(String parameterName) throws SQLException
    {
        return callableStatement.getDouble(parameterName);
    }
    
    /**
     * getDataSet
     * 
     * @param parameterName
     * @return
     * @throws SQLException
     */
    public ResultSet getDataSet(String parameterName) throws SQLException
    {
        resultSet = (ResultSet) callableStatement.getObject(parameterName);
        return resultSet;
    }
    
    public DataSource getDataSource()
    {
        return dataSource;
    }
    
    public void setDataSource(DataSource dataSource)
    {
        this.dataSource = dataSource;
    }
}
